package com.example.mobile203zlw20216061300618;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class UsersRViewAdapter extends RecyclerView.Adapter<UsersRViewAdapter.ViewHolder> {
    OnClickInterface clickInterface;
    interface OnClickInterface{
        public void onclick(View view,int position);
    }
    @NonNull
    private List<Users>list;
    public UsersRViewAdapter(@NonNull List<Users> list) {
        this.list = list;
    }
    public void setClickInterface(OnClickInterface clickInterface){
        this.clickInterface=clickInterface;
    }
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_user,null));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Users student=list.get(position);
        holder._id.setText(""+student.get_id());
        holder.name.setText(""+student.getName());
        holder.age.setText(""+student.getAge());
        holder.score.setText(""+student.getScore());
        holder.buttonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickInterface!=null){
                    clickInterface.onclick(v,position);
                }
            }
        });
        holder.buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickInterface!=null){
                    clickInterface.onclick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView _id, name, age,score;
        private Button buttonUpdate,buttonDelete;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            _id=itemView.findViewById(R.id._id);
            name=itemView.findViewById(R.id.name);
            age=itemView.findViewById(R.id.age);
            score=itemView.findViewById(R.id.score);
            buttonUpdate=itemView.findViewById(R.id.Update);
            buttonDelete=itemView.findViewById(R.id.Delete);
        }
    }
}
